/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.dialog;

import javafx.collections.ObservableList;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 页面弹出框容器组件
 * @author zpy
 * @version v1.0.0
 */
@Slf4j
public class PesDialogLayout extends VBox {
    private final StackPane heading = new StackPane();
    private final StackPane body = new StackPane();
    private final FlowPane actions = new FlowPane();

    protected final Boolean showActions;

    private double xPosition;
    private double yPosition;

    public PesDialogLayout(){
        this(true);
    }

    public PesDialogLayout(boolean showActions) {
        this.initialize();
        this.showActions = showActions;
        this.heading.setOnMousePressed(this::onMousePressed);
        this.heading.setOnMouseDragged(this::onMouseDragged);
        this.heading.setOnMouseReleased(this::setOnMouseReleased);
        if(showActions){
            this.getChildren().setAll(this.heading, this.body, this.actions);
        }else{
            this.getChildren().setAll(this.heading, this.body);
        }
    }

    private void onMousePressed(MouseEvent event){
        event.consume();
        //this.setCursor(Cursor.MOVE);
    }

    private void onMouseDragged(MouseEvent event){
        event.consume();
        this.setLayoutX(event.getScreenX());
        this.setLayoutY(event.getScreenY());
    }

    private void setOnMouseReleased(MouseEvent event){
        event.consume();
        this.setCursor(Cursor.DEFAULT);
    }

    public StackPane getHeading() {
        return this.heading;
    }

    public void setHeading(Node... titleContent) {
        this.heading.getChildren().setAll(titleContent);
    }

    public ObservableList<Node> getBody() {
        return this.body.getChildren();
    }

    public ObservableList<String> getBodyStyleClass(){
        return this.body.getStyleClass();
    }

    public void setBody(Node... body) {
        this.body.getChildren().setAll(body);
    }

    public ObservableList<Node> getActions() {
        return this.actions.getChildren();
    }

    public void setActions(Node... actions) {
        this.actions.getChildren().setAll(actions);
    }

    public void setActions(List<? extends Node> actions) {
        this.actions.getChildren().setAll(actions);
    }

    private void initialize() {
        this.getStyleClass().add("pes-dialog-layout");
        this.heading.getStyleClass().addAll("pes-layout-heading");
        this.body.getStyleClass().add("pes-layout-body");
        VBox.setVgrow(this.body, Priority.SOMETIMES);
        this.actions.getStyleClass().add("pes-layout-actions");
    }
}